package com.example.aiyou.passwordmanage.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.aiyou.passwordmanage.R;
import com.example.aiyou.passwordmanage.base.ActivityManage;
import com.example.aiyou.passwordmanage.base.BaseActivity;
import com.example.aiyou.passwordmanage.pojo.Password;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

public class SettingsActivity extends BaseActivity {
    private String [] setting = {"常用信息设置","重设密码","清空数据"};
    private UserActivity userActivity;
    private PasswordActivity passwordActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SettingsActivity.this,android.R.layout.simple_list_item_1,setting);
        ListView listView = (ListView)findViewById(R.id.setting_view);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(0 == i){
                    userActivity.actionStart(SettingsActivity.this);
                }else if(1 == i){
                    //passwordActivity.actionStart(SettingsActivity.this);
                    Intent intent = new Intent(SettingsActivity.this,PasswordActivity.class);
                    startActivity(intent);
                }else if(2 == i){
                    deleteAll();
                }
            }
        });
        Intent intent = getIntent();
        String meg = intent.getStringExtra("meg");
        if(!TextUtils.isEmpty(meg) && "success".equals(meg)){
            Toast.makeText(SettingsActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteAll() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //builder.setTitle("确认");
        builder.setMessage("确定要清空数据吗");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                DataSupport.deleteAll(Password.class);
                Toast.makeText(SettingsActivity.this,"清除成功",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        builder.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_settings,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem){
        int id = menuItem.getItemId();
        if(id == R.id.action_home){
            Intent intent = new Intent(SettingsActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
            return true;
        }else if(id == R.id.action_exit){
            ActivityManage.finishAll();
            finish();
            return true;
        }else if(id == R.id.action_about){
            Intent intent = new Intent(SettingsActivity.this,AboutActivity.class);
            startActivity(intent);
            finish();
            return true;
        }
        finish();
        return super.onOptionsItemSelected(menuItem);
    }
    @Override
    public void onBackPressed(){
        Intent intent = new Intent();
        intent.putExtra("meg","NiXiaoGou");
        setResult(RESULT_OK,intent);
        finish();
    }

}
